package com.pinc.exam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class Group_List extends WidgetActivity {
	private ListView lv;
	private DBClass dbHelper;
	private Cursor mCursor;
	private Group_Adapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		initWidget();

		dbHelper = new DBClass(this);
	}


	@Override
	protected void onResume() {
		super.onResume();
		showGroups();
	}

	private void showGroups()
	{
		mAdapter = new Group_Adapter(this);

		mCursor = dbHelper.getRecord(DBClass.TABLE_NAME_GROUPS, null, null, null, null);

		if (mCursor.moveToFirst())
		{
			do{
				Group_Class pc = new Group_Class(
						mCursor.getInt(mCursor.getColumnIndex("_id")),
						mCursor.getString(mCursor.getColumnIndex(DBClass.COL_GROUPS_NAME)),
						dbHelper.getQuestionNoInGroup(mCursor.getInt(mCursor.getColumnIndex("_id")))
				);
				mAdapter.add(pc);
			}while(mCursor.moveToNext());
		}
		mCursor.close();
		dbHelper.mDB.close();

		lv.setAdapter(mAdapter);
		lv.setOnItemClickListener(new ItemClickListener());
	}

	private class ItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
			Intent intent = new Intent(Group_List.this, Group_Content.class);
			intent.putExtra("id", id);
			startActivity(intent);
		}
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		lv = (ListView) this.findViewById(R.id.listView1);

		/* set upbar */
		tv_title.setText(getResources().getString(R.string.str_group));
		b_next.setVisibility(View.VISIBLE);
		b_next.setText(getResources().getString(R.string.str_new));
		b_next.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(ID_INPUT);
			}
		});
		b_prev.setVisibility(View.VISIBLE);
		b_prev.setText(getResources().getString(R.string.str_back));
		b_prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private static final int ID_INPUT = 0;
	private static final int ID_NAME_LEN = 1;
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch(id)
		{
		case ID_INPUT:
		{
			LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final EditText input = (EditText) inflater.inflate(R.layout.edittext_group_name, null);

			builder.setMessage(getResources().getString(R.string.str_create_group))
			.setView(input)
			.setPositiveButton(getResources().getString(R.string.str_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (input.getText().length() < 1)
						showDialog(ID_NAME_LEN);
					else
					{
						dbHelper.addGroup(input.getText().toString());
						showGroups();
					}
					dialog.cancel();
					input.setText("");
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			break;
		}
		case ID_NAME_LEN:
		{
			builder.setMessage(getResources().getString(R.string.str_invalid_name))
			.setPositiveButton(getResources().getString(R.string.str_ok), null)
			.setCancelable(false);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}

}
